/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.jfonia.view.symbols;

import java.awt.Graphics2D;
import java.awt.geom.QuadCurve2D;
import org.jfonia.constants.SymbolConstants;

/**
 *
 * @author Rik Bauwens
 */
public class TieSymbol extends GroupSymbol
{
        private ISymbol leftSymbol;
        private ISymbol rightSymbol;
        /*private MutableNode<Double> postXNode;
        private MutableNode<Double> postYNode;

        private Map<ISymbol, DoubleMinRelation> xRelations;
        private Map<ISymbol, DoubleMaxRelation> postXRelations;
        private Map<ISymbol, DoubleMinRelation> yRelations;
        private Map<ISymbol, DoubleMaxRelation> postYRelations;*/

        public TieSymbol()
        {
                super();
                /*postXNode = new BasicNode<Double>(0.0);
                postYNode = new BasicNode<Double>(0.0);

                MutableNode<Double> invertedXNode = new BasicNode<Double>(0.0);
                new DoubleInversionRelation(getXNode(), invertedXNode);
                new DoubleSumRelation(postXNode, invertedXNode, getWidthNode());
                MutableNode<Double> invertedYNode = new BasicNode<Double>(0.0);
                new DoubleInversionRelation(getYNode(), invertedYNode);
                new DoubleSumRelation(postYNode, invertedYNode, getHeightNode());

                xRelations = new HashMap<ISymbol, DoubleMinRelation>();
                postXRelations = new HashMap<ISymbol, DoubleMaxRelation>();
                yRelations = new HashMap<ISymbol, DoubleMinRelation>();
                postYRelations = new HashMap<ISymbol, DoubleMaxRelation>();*/
        }

        public void setLeftSymbol(ISymbol symbol)
        {
                this.leftSymbol = symbol;
        }

        public void setRightSymbol(ISymbol symbol)
        {
                this.rightSymbol = symbol;
        }

        /*public void addSymbol(ISymbol symbol)
        {
                xRelations.put(symbol, new DoubleMinRelation(getXNode(), symbol.getAbsoluteXNode()));
                MutableNode<Double> symbolPostXNode = new BasicNode<Double>(0.0);
                new UnidirectionalDoubleSumRelation(symbol.getAbsoluteXNode(), symbol.getWidthNode(), postXNode);
                postXRelations.put(symbol, new DoubleMaxRelation(getXNode(), symbolPostXNode));

                yRelations.put(symbol, new DoubleMinRelation(getYNode(), symbol.getAbsoluteYNode()));
                MutableNode<Double> symbolPostYNode = new BasicNode<Double>(0.0);
                new UnidirectionalDoubleSumRelation(symbol.getAbsoluteYNode(), symbol.getHeightNode(), postYNode);
                postYRelations.put(symbol, new DoubleMaxRelation(getYNode(), symbolPostYNode));
        }

        public void removeSymbol(ISymbol symbol)
        {
                if(xRelations.containsKey(symbol))
                {
                        xRelations.get(symbol).switchOff();
                        xRelations.remove(symbol);
                }
                if(postXRelations.containsKey(symbol))
                {
                        postXRelations.get(symbol).switchOff();
                        postXRelations.remove(symbol);
                }
                if(yRelations.containsKey(symbol))
                {
                        yRelations.get(symbol).switchOff();
                        yRelations.remove(symbol);
                }
                if(postYRelations.containsKey(symbol))
                {
                        postYRelations.get(symbol).switchOff();
                        postYRelations.remove(symbol);
                }
        }*/

        public void paintSymbol(Graphics2D g)
        {
                //g.draw(new QuadCurve2D.Float((int) getX(), (int) getY(), 80, 80,
                        //postXNode.getValue().intValue(), postYNode.getValue().intValue()));
                if(leftSymbol != null && rightSymbol != null)
                {
                        double leftX = leftSymbol.getAbsoluteXNode().getValue();
                        double leftY = SymbolConstants.SLUR_SPACING;
                        if(leftSymbol instanceof IStaffSymbol)
                                leftY += ((IStaffSymbol) leftSymbol).getFloorYNode().getValue();
                        else
                                leftY += leftSymbol.getAbsoluteXNode().getValue();

                        double rightX = rightSymbol.getAbsoluteXNode().getValue();
                        double rightY = SymbolConstants.SLUR_SPACING;
                        if(rightSymbol instanceof IStaffSymbol)
                                rightY += ((IStaffSymbol) rightSymbol).getFloorYNode().getValue();
                        else
                                rightY += rightSymbol.getAbsoluteXNode().getValue();
                        for(double offset = SymbolConstants.SLUR_THICKNESS; offset > 0; offset -= 2.0)
                        {
                                g.draw(new QuadCurve2D.Float((int) leftX, (int) leftY,
                                                             (int) (leftX + (rightX - leftX) / 2),
                                                             (int) (Math.max(leftY, rightY)
                                                                    + SymbolConstants.SLUR_HEIGHT + offset),
                                                             (int) rightX, (int) rightY));
                        }
                }

        }
}